<template>
	<view class="container">
		<view class="tip">
			当前{{info.mobile}}账户正在修改密码，请提前获取验证码
		</view>
		<view class="code">
			<input type="text" v-model="code" placeholder="请输入验证码" placeholder-class="cl" />
			<view class="btn" @click="getyzm()" :style="{color:btnBool?'#5398E5':'#CACACA'}">{{yzmtext}}</view>
		</view>
		<view class="code">
			<input type="password" v-model="pass" value="" placeholder="请输入密码" placeholder-class="cl" />
		</view>
		<view :class="['save',{'active':code!==''&&pass!==''}]" @click="save" >
			确认
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				btnBool:true,
				yzmtext:"获取验证码",
				info:"",
				code:"",
				pass:"",
				token:"",
			}
		},
		created() {
			uni.showLoading({
				title: '加载中'
			});
			uni.getStorage({
				key: 'user_info',
				success: (res) => {
					this.token = res.data.token;
					this.getData()
				},
				fail: (res) => {
					uni.redirectTo({
						url: '../../login/auth'
					});
				}
			});
		},
		methods: {
			getyzm() {
				if(this.btnBool){
					this.btnBool = false;
					let i = 60;
					let timer = setInterval(() => {
						this.yzmtext = '获取验证码' + i + 's';
						i--;
						if (i < 0) {
							this.btnBool = true;
							this.yzmtext = '获取验证码';
							clearInterval(timer);
						}
					}, 1000);
					uni.request({
							url: `https://mx.acktechnologies.com.cn/api/wxapp/mine/info/changePassSmsCode`,
							data: {
								"token": this.token
							},
							method: 'GET',
							success: res => {
								console.log(res)
								if (res.data.code === 1) {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
								} else if (res.data.code === 403) {
									uni.redirectTo({
										url: '../../login/auth'
									});
								} else {
									uni.showToast({
										title: '服务器开小差了呢，请您稍后再试',
										icon: 'none'
									})
								}
							},
							fail: () => { //接口请求失败的处理
								uni.showToast({
									title: '服务器开小差了呢，请您稍后再试',
									icon: 'none'
								})
							},
						});
				}else{
					return false;
				}
			},
			save() {
				if (this.code === '') {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return false;
				}
				if (this.pass === '') {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return false;
				}
				let param = {
					"token": this.token,
					"smscode": this.code,
					"password":this.pass
				}
				uni.request({
					url: `https://mx.acktechnologies.com.cn/api/wxapp/mine/info/changePass`,
					data: param,
					method: 'GET',
					success: res => {
						console.log(res)
						if (res.data.code === 1) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						} else if (res.data.code === 403) {
							uni.redirectTo({
								url: '../../login/auth'
							});
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					fail: () => { //接口请求失败的处理
						uni.showToast({
							title: '服务器开小差了呢，请您稍后再试',
							icon: 'none'
						})
					},
				});
			},
			getData() {
				uni.request({
					url: `https://mx.acktechnologies.com.cn/api/wxapp/mine/info/myinfo`,
					data: {
						"token": this.token
					},
					method: 'GET',
					success: res => {
						console.log(res)
						if (res.data.code === 1) {
							let info = res.data.data;
							this.info = info;
							uni.hideLoading();
						} else if (res.data.code === 403) {
							uni.redirectTo({
								url: '../../login/auth'
							});
						} else {
							uni.showToast({
								title: '服务器开小差了呢，请您稍后再试',
								icon: 'none'
							})
						}
					},
					fail: () => { //接口请求失败的处理
						uni.showToast({
							title: '服务器开小差了呢，请您稍后再试',
							icon: 'none'
						})
					},
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	.container {
		padding: 20rpx 60rpx 0 48rpx;
		box-sizing: border-box;

		.tip {
			width: 598rpx;
			font-size: 15px;
			color: #666666;
			line-height: 24px;
			margin: 0 auto;
			text-align: justify;
		}

		.code {
			width: 642rpx;
			height: 70rpx;
			border-bottom: 1px solid #EDEDED;
			position: relative;
			margin-top: 40px;
			overflow: hidden;

			.btn {
				position: absolute;
				bottom: 20%;
				right: 0;
				font-size: 17px;
				color: #CACACA;
				z-index: 99;
			}

			input {
				height: 100%;
			}
		}

		.cl {
			font-size: 15px;
			color: #CACACA;
		}
	}
	.save {
		width: 670rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background: #E3E3E3;
		border-radius: 40rpx;
		margin: 60px auto 0 auto;
		font-size: 18px;
		color: #FFFFFF;
	}
	.active{
		background: linear-gradient(90deg, #9BDFB5 0%, #84DFD1 100%);
	}
</style>
